package com.xkstream.client.ui.kt

import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Message
import android.widget.Button
import android.widget.ImageView
import android.widget.ScrollView
import android.widget.TextView
import butterknife.BindString
import butterknife.BindView
import butterknife.ButterKnife
import com.xkstream.client.R
import com.xkstream.client.ZegoApiManager
import com.xkstream.client.api.API
import com.xkstream.client.application.Application
import com.xkstream.client.base.BaseActivity
import com.xkstream.client.base.BasePresenter
import com.xkstream.client.entity.User
import com.xkstream.client.http.FileUploadUtil
import com.xkstream.client.presenter.UserContract
import com.xkstream.client.ui.AnchorApplyActivity
import com.xkstream.client.utils.BitmapUtil
import com.zego.zegoliveroom.ZegoLiveRoom
import java.io.File
import java.lang.StringBuilder
import java.lang.ref.WeakReference
import java.util.HashMap

/**
 * Created by xiaoyi on 2017-11-7.
 */
class UploadLogFileActivity : BaseActivity<BasePresenter>() {

    lateinit var log: TextView


    lateinit var btnUpload: Button

    lateinit var logFiles: String

    var mHandler: MHandler? = null

    var list: ArrayList<String> = ArrayList()

    val DIR = Environment.getExternalStorageDirectory().absolutePath + File.separator + "Android/data/"

    lateinit var back: ImageView

    override fun initViews(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_uploadlog)
        ButterKnife.bind(this@UploadLogFileActivity)

        setTitle(R.string.upload_log)

        log = findViewById(R.id.log) as TextView

        btnUpload = findView(R.id.btnUpload)

        back = findView(R.id.headerBack)

        back.setOnClickListener { _ -> finish() }

        logFiles = resources.getString(R.string.log_files)

        var pathStr = DIR + packageName + File.separator + "files" + File.separator

        var files: Array<File> = File(pathStr).listFiles()
        var sb = StringBuilder(logFiles + "\n\n")
        for (file in files) {
            if (file.absolutePath.contains("zego")||file.absolutePath.contains("legend_log")) {
                sb.append(file.absolutePath).append("\n\n")
                list.add(file.absolutePath)
            }
        }
        log.setText(sb.toString())


        mHandler = MHandler(this)

        btnUpload.setOnClickListener({ _ ->



            for (file in list) {
                val params = HashMap<String, String>()
                val textParams = HashMap<String, String>()
                textParams.put("anchorId", Application.getUser().getAnchor().getAnchor_push_ur())
                val result = Uri.fromFile(File(file))
                params.put("iconFile", BitmapUtil.getPathByUri4kitkat(this, result))

                showLoading()
                Thread {
                    FileUploadUtil.uploadImageFile(API.FILEUPLOAD_HOST + API.UPLOAD_LOG,
                            textParams, params, mHandler, file)
                }.start()
            }
        })
    }


    companion object {
        class MHandler(activity: UploadLogFileActivity) : Handler() {

            var weakReference: WeakReference<UploadLogFileActivity> = WeakReference(activity)

            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                when (msg.what) {
                    FileUploadUtil.SUCCESS -> if (weakReference.get() != null) {
                        weakReference.get()?.hideLoading()
                        weakReference.get()?.showTopTips(R.string.upload_success)
                        weakReference.get()?.handleUpload(msg)

                    }

                    FileUploadUtil.FAILED -> if (weakReference.get() != null) {
                        weakReference.get()?.hideLoading()
                        weakReference.get()?.showTopTips(R.string.upload_failed)
                    }
                }
            }
        }
    }

    fun handleUpload(msg: Message) {
        val fileName: String = msg.obj.toString()
        val file = File(fileName)
        if (file.exists()) {
            file.delete()
        }
    }


}